package com.light.gamecomponents.physics;

import javax.microedition.khronos.opengles.GL10;

import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.extension.physics.box2d.PhysicsFactory;
import org.anddev.andengine.extension.physics.box2d.PhysicsWorld;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.light.gamecomponents.IGameComponents;
import com.light.roller.ResourceManager;
import com.light.roller.Roller;

public class SaveWall implements IGameComponents{
	
	protected Sprite m_sprite;
	protected Sprite m_sprite2;
	protected Body m_body;
	protected float m_rotation;
	protected Roller m_game;
	protected Vector2 m_InitPosition;
	

	public SaveWall(Roller game,Vector2 InitPosition,float rotation)
	{
	m_rotation = rotation;
		m_game = game;
		m_InitPosition = InitPosition;

	}
	
	public int getX1() {

		return (int) m_sprite.getX();
	}

	public int getX2() {

		return (int)(m_sprite.getX()+m_sprite.getWidth());
	}

	public int getY1() {

		return (int) m_sprite.getY();
	}

	public int getY2() {

		return (int)(m_sprite.getY()+m_sprite.getHeight());
	}
	
	@Override
	public void onLoadResources() {
		
		ResourceManager.onLoadResources("safe", m_game);
		ResourceManager.onLoadResources("safe2", m_game);
		
	}
	

	@Override
	public void onLoadScene(Scene scene, PhysicsWorld physicsWorld) { 
		
		FixtureDef objectFixtureDef = PhysicsFactory.createFixtureDef(PhysicsComponents.SAFE_EIGEN, 0.0f, 1.0f);
		
		objectFixtureDef.filter.categoryBits = Roller.CATEGORYBIT_WALL;
		objectFixtureDef.filter.maskBits = Roller.MASKBIT_WALL;
		
		m_sprite = new Sprite(m_InitPosition.x, 
							m_InitPosition.y,
							ResourceManager.getTextureRegion("safe"));
		
		m_sprite2 = new Sprite(0, -7,
				ResourceManager.getTextureRegion("safe2"));
		
		m_sprite.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		
		m_sprite.setAlpha(0);				
		
		m_sprite.setRotation(m_rotation);
		m_body = PhysicsFactory.createBoxBody(physicsWorld,
											  m_sprite, 
											  BodyType.StaticBody, 
											  objectFixtureDef);		
		
				
		
		scene.attachChild(m_sprite);
		scene.getLastChild().attachChild(m_sprite2);
		
		
		
		
		
		m_body.setType(BodyType.StaticBody);
		m_body.setLinearVelocity(new Vector2(0.0f, 0.0f));
		m_body.setAngularVelocity(0);
	}

	@Override
	public void onLoadEngine() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void unLoadResources() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void removeFromScene(Scene scene, PhysicsWorld physicsWorld) {
		scene.detachChild(m_sprite);
		physicsWorld.destroyBody(m_body);
	}
	@Override
	public void rotate(float angle) {
		// TODO Auto-generated method stub
		
	}
}
